<?php
namespace App\Controller;
use Common\Controller\AppuserbaseController;
use Think\Controller;

class PublicController extends AppuserbaseController {
    //用户获取短信
	public function getPhoneCode(){
        $phone=I('phone');
        $type=I('type',0,'intval');
        if(!$phone||!$type){
            $this->jsonReturn(0,'电话必填');
        }
        if(!lt_is_telephone($phone)){
            $this->jsonReturn(0,'电话格式不正确');
        }
        $user=M('User')->where(['phone'=>$phone])->field('id')->find();
        switch ($type){
            //注册需要手机号不存在
            case 1 :
                if($user){
                    $this->jsonReturn(0,'手机号已存在');
                }
                break;
            //忘记密码需要手机号存在
            case 2 :
                if(!$user){
                    $this->jsonReturn(0,'手机号不存在');
                }
                break;
            default :
                $this->jsonReturn(0,'非法数值');
        }
        $this->sendSmsAndLog($phone);
	}

    //关于我们
    public function getAboutUs(){
        $article=M('Posts')->where(['id'=>2])->find();
        $this->assign('article',$article['post_content']);
        $this->display('getAboutUs');
    }

    //客服电话
    public function getServicePhone(){
        $article=M('Posts')->where(['id'=>5])->find();
        $this->jsonReturn(1,'获取成功',$article['post_excerpt']);
    }

    //用户消息详情页
    public function getUserMessage(){
        $token=I('token');
        $id=I('id',0,'intval');
        if(!$token){
            $this->error('请先登陆');
        }
        if(!$id){
            $this->error('消息不存在');
        }
        $user=lt_get_user_by_token($token);
        if(!$user){
            $this->error('请先登陆');
        }
        if($user['status']==2){
            $this->error('请先绑定手机号');
        }
        if($user['status']!=1){
            $this->error('用户已被拉黑');
        }
        $message=M('Message')->where(['id'=>$id,'user_id'=>$user['id']])->find();
        if(!$message){
            $this->error('消息不存在');
        }
        if($message['is_read']==0){
            M('Message')->where(['id'=>$id])->save(['is_read'=>1]);
        }
        $this->assign('message',$message['content']);
        $this->display('getUserMessage');
    }

    //获取提现规则
    public function getBusinessBalanceRule(){
        $article=M('Posts')->where(['id'=>7])->find();
        $this->assign('article',$article['post_content']);
        $this->display('getBusinessBalanceRule');
    }
}